let fs = require('fs');
let loaderUtils = require('loader-utils');
let validateOptions = require('schema-utils');

function loader(source) {
    let options = loaderUtils.getOptions(this);
    let schema = {
        type: 'object',
        properties: {
            text: {
                type: 'string'
            },
            filename: {
                type: 'string'
            }
        }
    }
    // 格式校验 格式 校验对象 报错时报哪个名字
    validateOptions(schema, options, 'banner-loader');
    // 添加此loader要用的文件依赖
    this.addDependency(options.filename);
    let callback = this.async();
    if (options.filename) {
        fs.readFile(options.filename, 'utf8', (err, data) => {
            callback(err, `/**${data}**/${source}`)
        })
    } else {
        callback(null, `/**${options.text}**/${source}`)
    }
}

module.exports = loader